commit xxxx (genbasedir_invalidate)
Author: Ivan Zakharyaschev <imz@altlinux.org>
Date:   Mon Feb 15 05:18:39 2021 +0300

    genbasedir: for tests, simulate genpkglist being first to fill the file

    This is needed to demonstrate a potential race leading to an error
    when patching the basedir in-place.

    Currently, genpkglist creates the file, but does not fill it until it
    has read something from stdin. The code that pipes to its stdin
    depends on the file being empty not to choose it as the old pkglist.
    So, with the current implementation of genpkglist there is no error,
    but we would be more safe if we avoided choosing the invalid file
    independently of the implementation of genpkglist.

diff --git a/src/genbasedir b/src/genbasedir
index 61e4d00..344a471 100755
--- a/src/genbasedir
+++ b/src/genbasedir
@@ -370,6 +370,7 @@ save_file()
 	else
 		saved_list=
 	fi
+	echo 'Not ready. Do not read from this file! It is about to be written to.' >"$1"
 }
 
 compare_file()
@@ -530,7 +531,11 @@ if [ -z "$hashonly" ]; then
 		cat_prev_list_if_patching="$(choose_prev_list "$prev_basedir" pkglist "$comp")" ||
 			Fatal 'Failed to find the previous pkglist, but patching it was requested.'
 		eval "$cat_prev_list_if_patching" |
-			if ! genpkglist $progress $bloat $noscan \
+			if sleep .5
+			   # sleep not to let genpkglist truncate the output file
+			   # immediately, give the code on the other end of the pipe
+			   # a chance to choose it as the input while it's still non-empty.
+			   ! genpkglist $progress $bloat $noscan \
 					--index "$SRCIDX_COMP" \
 					${patch_basedir:+--prev-stdin} \
 					${comp_add_list:+--add-list "$comp_add_list"} \
@@ -567,7 +572,11 @@ if [ -z "$hashonly" ]; then
 		cat_prev_list_if_patching="$(choose_prev_list "$prev_basedir" srclist "$comp")" ||
 			Fatal 'Failed to find the previous srclist, but patching it was requested.'
 		eval "$cat_prev_list_if_patching" |
-			if ! gensrclist $progress $flat $mapi \
+			if sleep .5
+			   # sleep not to let gensrclist truncate the output file
+			   # immediately, give the code on the other end of the pipe
+			   # a chance to choose it as the input while it's still non-empty.
+			   ! gensrclist $progress $flat $mapi \
 					${patch_basedir:+--prev-stdin} \
 					${comp_add_list_src:+--add-list "$comp_add_list_src"} \
 					${comp_rm_list_src:+--rm-list "$comp_rm_list_src"} \
